<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-tabs v-model:activeKey="activeKey">
			<a-tab-pane key="user" tab="用户信息" force-render>
				<a-form ref="userFormRef" :model="formData" :rules="formRules" layout="vertical">
					<a-form-item label="名称：" name="nickName">
						<a-input v-model:value="formData.nickName" placeholder="请输入名称" allow-clear />
					</a-form-item>
					<a-form-item label="生日日期：" name="birthday">
						<a-date-picker v-model:value="formData.birthday" format="YYYY-MM-DD" placeholder="请输入生日日期" />
					</a-form-item>
					<a-form-item label="性别：" name="sex">
						<a-radio-group v-model:value="formData.sex" :options="genderOptions"> </a-radio-group>
					</a-form-item>
					<a-form-item label="手机号：" name="phone">
						<a-input v-model:value="formData.phone" placeholder="请输入手机号" allow-clear />
					</a-form-item>
					<a-form-item label="openid：" name="openid">
						<a-input v-model:value="formData.openid" placeholder="请输入openid" allow-clear />
					</a-form-item>
				</a-form>
			</a-tab-pane>
			<a-tab-pane key="member" tab="会员信息" force-render>
				<a-form ref="memberFormRef" :model="formData" :rules="formRules" layout="vertical">
					<a-form-item label="会员卡号：" name="code">
						<a-input disabled v-model:value="formData.code" placeholder="请输入会员卡号" allow-clear />
					</a-form-item>
					<a-form-item label="会员等级：" name="memberRank">
						<a-select
							v-model:value="formData.memberRank"
							:options="memberRankOptions"
							placeholder="请选择会员等级"></a-select>
					</a-form-item>
					<a-form-item label="储值余额：" name="amount">
						<a-input-number disabled v-model:value="formData.amount" />
					</a-form-item>
					<a-form-item label="储值本金：" name="amountRecharge">
						<a-input-number disabled v-model:value="formData.amountRecharge" />
					</a-form-item>
					<a-form-item label="赠送金：" name="amountPresent">
						<a-input-number disabled v-model:value="formData.amountPresent" />
					</a-form-item>
				</a-form>
			</a-tab-pane>
		</a-tabs>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="crmMemberForm">
	import dayjs from 'dayjs'
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	// 默认是关闭状态
	let visible = $ref(false)
	const emit = defineEmits({ successful: null })
	const userFormRef = ref()
	const memberFormRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	const activeKey = ref('user')
	// 打开抽屉
	const onOpen = (record) => {
		visible = true
		formData.value = {
			canBonus: true,
			canRecharge: true,
			canDiscount: true,
			type: 'FREE',
			sortCode: 99
		}
		activeKey.value = 'user'
		if (record) {
			formData.value = Object.assign({}, record)
			console.log(formData.value)
			formData.value.sex += ''
			if (formData.value.birthday) {
				formData.value.birthday = dayjs(formData.value.birthday)
			}
			formData.value.canBonus = record.canBonus === '1'
			formData.value.canRecharge = record.canRecharge === '1'
			formData.value.canDiscount = record.canDiscount === '1'
		}
	}
	// 关闭抽屉
	const onClose = () => {
		userFormRef.value.resetFields()
		if (memberFormRef.value) {
			memberFormRef.value.resetFields()
		}
		visible = false
	}

	// 默认要校验的
	const formRules = {
		code: [required('请输入会员卡号')],
		name: [required('请输入会员等级名称')],
		memberRank: [required('请选择会员类型')]
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}
	const { proxy } = getCurrentInstance()
	const memberRankOptions = proxy.$TOOL
		.tableList('CRM_MEMBER_RANK')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})
	const genderOptions = proxy.$TOOL.dictTypeList('USER_GENDER')
		.map((item) => {
			return {
				value: item['dictValue'],
				label: item['name']
			}
		})
		.sort((x, y) => {
			return x.value <= x.value
		})
	// 验证并提交数据
	const onSubmit = () => {
		userFormRef.value
			.validate()
			.then(() => {
				memberFormRef.value.validate().then(() => {
					const param = Object.assign({}, formData.value)
					if (param.birthday) {
						param.birthday = dayjs(param.birthday).format('YYYY-MM-DD')
					}
					submitLoading.value = true
					crmApi.memberCardSubmitForm(param, param.id).then(() => {
						visible = false
						emit('successful')
					}).finally(() => {
						submitLoading.value = false
					})
				})
			})
	}
	const parameterChanges = (data) => {
		return data
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
